package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.VO.AirSiteVO;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.AirSiteEntity;
import com.iwomy.envpro.platform.entity.CodeAreaEntity;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.AirSiteService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 监测点
 *
 * @author pig
 * @date 2023-08-26 14:22:49
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/airSite")
@Tag(description = "airSite", name = "监测点管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AirSiteController {

	private final AirSiteService airSiteService;

	private final AirMonitorEquipmentService airMonitorEquipmentService;

	/**
	 * 分页查询
	 *
	 * @param page    分页对象
	 * @param airSite 监测点
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_airSite_view')")
	public R getAirSitePage(@ParameterObject Page page, @ParameterObject AirSiteEntity airSite) {
		LambdaQueryWrapper<AirSiteEntity> wrapper = Wrappers.lambdaQuery(airSite);
		return R.ok(airSiteService.page(page, wrapper));
	}


	/**
	 * 通过id查询监测点
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_airSite_view')")
	public R getById(@PathVariable("id") String id) {
		return R.ok(airSiteService.getById(id));
	}

	/**
	 * 新增监测点
	 *
	 * @param airSite 监测点
	 * @return R
	 */
	@Operation(summary = "新增监测点", description = "新增监测点")
	@SysLog("新增监测点")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_airSite_add')")
	public R save(@RequestBody AirSiteEntity airSite) {
		return R.ok(airSiteService.save(airSite));
	}

	/**
	 * 修改监测点
	 *
	 * @param airSite 监测点
	 * @return R
	 */
	@Operation(summary = "修改监测点", description = "修改监测点")
	@SysLog("修改监测点")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_airSite_edit')")
	public R updateById(@RequestBody AirSiteEntity airSite) {
		long count = airMonitorEquipmentService.count(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().eq(AirMonitorEquipmentEntity::getAirSiteId, airSite.getId()));
		if (count > 0)
			return R.failed("区域已关联设备，不允许编辑");
		return R.ok(airSiteService.updateById(airSite));
	}

	/**
	 * 通过id删除监测点
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除监测点", description = "通过id删除监测点")
	@SysLog("通过id删除监测点")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_airSite_del')")
	public R removeById(@RequestBody Long[] ids) {
		long count = airMonitorEquipmentService.count(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().in(AirMonitorEquipmentEntity::getAirSiteId, ids));

		if (count > 0)
			return R.failed("区域已关联设备，不允许删除");

		return R.ok(airSiteService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param airSite 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_airSite_export')")
	public List<AirSiteEntity> export(AirSiteEntity airSite) {
		return airSiteService.list(Wrappers.query(airSite));
	}


	/**
	 * @param airSite 查询条件
	 */
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_airSite_export')")
	public R list(AirSiteEntity airSite) {
		return R.ok(airSiteService.list(Wrappers.query(airSite)));
	}


	/**
	 * 获取指定区域下的站点
	 *
	 * @param page
	 * @param tag
	 * @return
	 */
	@GetMapping("/getAirSiteList")
	@PreAuthorize("@pms.hasPermission('platform_airSite_view')")
	public R getAirSiteList(Page page, @RequestParam("tag") Long tag) {
		return R.ok(airSiteService.getAirSitePage(page, tag));
	}

	/**
	 * 获取所有站点 和行政区划
	 *
	 * @return
	 */
	@GetMapping("/getAirSiteListAll")
//	@PreAuthorize("@pms.hasPermission('platform_airSite_view')" )
	public R getAirSiteListAll() {
		List<AirSiteVO> airMonitorEquipmentListAll = airSiteService.getAirSiteListAll(null);
		return R.ok(airMonitorEquipmentListAll);
	}

	/*****************APP接口***********************/

	/**
	 * APP只要 区、镇、检测点
	 * 分页查询
	 *
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/appAirSitePage")
	public R appAirSitePage() {
		//APP只要 区、镇、检测点
		List<AirSiteVO> airMonitorEquipmentListAll = airSiteService.appAirSitePage();
		return R.ok(airMonitorEquipmentListAll);
	}

	/**
	 * 获取所有站点 和行政区划
	 *
	 * @return
	 */
	@GetMapping("/appAirSiteListAll")
	public R appAirSiteListAll() {
		List<AirSiteVO> airMonitorEquipmentListAll = airSiteService.getAirSiteListAll(null);
		return R.ok(airMonitorEquipmentListAll);
	}

}